package com.yc.tomcat2.javax.dangdangwan;

import com.yc.tomcat2.javax.servlet.http.YcHttpServletRequest;
import com.yc.tomcat2.javax.servlet.http.YcHttpServletResponse;
import com.yc.tomcat2.javax.servlet.YcWebServlet;
import com.yc.tomcat2.javax.servlet.http.YcHttpServlet;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

/**
 * @author zwei
 * @create 2024-07-27 20:19
 */
@YcWebServlet("/hello")
public class HelloServlet extends YcHttpServlet {
    public HelloServlet(){
        System.out.println("hello的构造方法");
    }

    @Override
    public void init() {
        System.out.println("hello的init方法");
        super.init();
    }

    @Override
    public void doGet(YcHttpServletRequest req, YcHttpServletResponse resp) {
//        System.out.println("hello，你好");
        String result="hello,你好";


        PrintWriter out=resp.getWriter();
        //标准的tomcat是由tomcat服务器来完成这个响应的构建
        try {
            out.println("HTTP/1.1 200 OK\r\nContent-Type: text/html;charset=utf-8\r\nContent-length: "+result.getBytes(  "utf-8"  ).length+"\r\n\r\n");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        out.println(result);
        out.flush();
        out.close();
    }
}
